home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programmer Power Tools
/
Programmer Power Tools.iso
/
progjrn
/
pj_6_3.arc
/
TIMER2
< prev
Wrap
Text File
|
1988-04-05
|
5KB
|
201 lines
Code from Redirections, "Pink Noise" by Frank D. Greco.
Copyright 1988 by Frank D. Greco. No commercial use of this code
without express permission of the author.
1 /*
2 * clock.h -- (real) Quick and (really) Dirty macros
3 */
4
5 #define MSN(x) ( (x) >> 4 )
6 #define LSN(x) ( (x) & 0x0f )
7 #define LSB(x) ( (x) & 0x00ff )
8 #define MSB(x) ( (x) >> 8 )
9
10 #define BCD(x) ( (((x) / 10) << 4) | ((x) % 10) )
11 #define UNBCD(x) (MSN(x)*10 + LSN(x))
12
13 #define CLOCK_RATE 1193180 /* 1,193,180 Hz or 1.19 MHz */
14 #define STD_PULSE -1 /* 65536 or 0xFFFF */
15 #define RESTORE_PULSE() setpulse(STD_PULSE);
1 /*
2 * test.c -- Driver program to show how to use new timing facilities
3 *
4 * For the PC, compile as follows:
5 *
6 * C:\> cl -DPC -DFAST_CLOCK test.c gettime.c setpulse.c fixtix.c
7 */
8
9 #include <stdio.h>
10 #include <process.h>
11 #include <dos.h>
12 #include "timer.h"
13 #include "clock.h"
14
15 #define NO_ERR 0 /* No error to return to the shell */
16 #define NO_ARGS 1 /* If nothing to time, return this value */
17
18 main(argc,argv)
19 char **argv;
20 {
21 char *bp; /* Ptr to command to be spawned */
22 unsigned long gettime(); /* Gets clock ticks from MS-DOS */
23 int i;
24
25 if (argc == 1) /* If nothing to time, bye-bye */
26 exit(NO_ARGS);
27
28 bp = argv[1];
29
30 #ifdef PC
31 setpulse(1000); /* Set timing granularity to 1/1000 of a second */
32 #endif
33
34 /* Test #1 */
35 TIME(spawnvp(P_WAIT, bp, ++argv), bp);
36
37 /* Test #2 */
38 START();
39 for (i=0; i<250;i++)
40 {
41 printf("\r %d ", i);
42 }
43 STOP();
44 PRINT_TIME("for loop");
45
46 /* Test #3 */
47 TIME(printf("Hello World\n"), "printf call");
48
49 /* Test #4 */
50 TIME(func1(), "Call to func1() using integer variable");
51
52 /* Test #5 */
53 TIME(func2(), "Call to func2() using register variable");
54
55 #ifdef PC
56 setpulse(STD_PULSE);/* Reset timing granularity back to 1/18 of a second */
57 fixtix(); /* Adjust BIOS view of time */
58 #endif
59 exit(NO_ERR); /* Good Housekeeping */
60 }
61 /*****************************************/
62 func1()
63 {
64 int i;
65
66 for (i = 0; i <1000; i++)
67 ;
68 }
69 /*****************************************/
70 func2()
71 {
72 register int i;
73
74 for (i = 0; i <1000; i++)
75 ;
76 }
1 /*
2 * gettime.c -- Uses BIOS interrupt 0x1A to get the timer count
3 * in clock ticks.
4 */
5
6 #include <dos.h>
7
8 #define TOD_INT 0x1a
9
10 unsigned long gettime()
11 {
12 union REGS in, out;
13 unsigned long current_tix; /* 32-bit tick value to be returned */
14
15 in.h.ah = 0; /* AH = 0 GET clock ticks */
16 int86(TOD_INT, &in, &out); /* Call BIOS timer tick interrupt */
17
18 current_tix = ( (long) out.x.cx << 16 ) + out.x.dx;
19
20 /* If midnite passes, add a day's worth of clock tix to the return value */
21
22 return ( out.x.ax & 0xff ) ? current_tix += 0x01800b0L: current_tix;
23 }
1 /*
2 * fixtix.c -- Reset the BIOS view of the clock ticks.
3 * Needed after programming the timer chip to pulse faster.
4 */
5
6 #include <dos.h>
7 #include <stdio.h>
8 #include "clock.h"
9
10 #define AH regs.h.ah
11 #define CH regs.h.ch
12 #define CL regs.h.cl
13 #define DH regs.h.dh
14 #define DL regs.h.dl
15
16 union REGS regs;
17
18 fixtix()
19 {
20 long far *fp;
21 long hrs, mins, secs, hsecs, total, tix;
22
23 FP_SEG(fp) = 0x0040; /* Point to BIOS data area where tix are stored */
24 FP_OFF(fp) = 0x006C;
25
26 AH = 2; /* GET real time clock */
27
28 int86(0x1a, ®s, ®s);
29
30 /* Convert everything to 1/100 seconds */
31
32 hrs = (long) 60 * 60 * 100 * UNBCD(CH);
33 mins = (long) 60 * 100 * UNBCD(CL);
34 secs = (long) 100 * UNBCD(DH);
35 hsecs = (long) UNBCD(DL);
36
37 /* Total them up and convert to clock ticks */
38
39 total = hrs + mins + secs + hsecs;
40 tix = (long) total * .1820648;
41
42 /* "Poke" the tick value into the BIOS data area */
43
44 *fp = tix;
45 }
1 /*
2 * setpulse.c -- Will program the 8254 to Pulse at a desired rate.
3 */
4 #include "clock.h"
5
6 setpulse(num)
7 int num; /* desired timing resolution */
8 {
9 int ax;
10
11 ax = (num == STD_PULSE ? 0xFFFF : (CLOCK_RATE / num));
12
13 outp( 0x43, 0x36); /* 0011 0110 */
14 outp( 0x40, LSB(ax) ); /* First the Least Significant Byte */
15 outp( 0x40, MSB(ax) ); /* Then the MSB */
16 }